<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');
/*
 * To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
 * Description of dashboard
 *
 * @author Subha Sundar Das (subhasundar.das@gmail.com)
 */
class Settings extends MX_Controller {



	function settings() {
		parent :: __construct();

		$this->load->model(array('admin_member_model','admin_settings_model'));

		$this->admin_member_model->check_admin_login();
		$this->template
		->set_layout('default') // application/views/layouts/two_col.php
		->set_theme('admin'); // application/views/layouts/two_col.php
		 
		$this->template->set_partial('nav_bar', 'layouts/nav_bar');
		$this->template->set_partial('side_menu', 'layouts/side_menu');
		$this->template->set_partial('footer','footer/footer');



		 
	}
	//--------Admin Web Settings----------------------





	function web_settings() {
		
		$sql = "select * from web_settings limit 1";
		$query = $this->db->query($sql);
		$data['web']['settings'] = $query->row();
		$this->template->build('settings/web_settings',$data);
	}
	
	function save(){
		if($this->input->post('save_settings')){
			
			$this->form_validation->set_rules('id','ID','required');
			$this->form_validation->set_rules('web_title','Website Name','trim|required|xss_clean');
			$this->form_validation->set_rules('tagline','Tagline','trim|required|xss_clean');
			$this->form_validation->set_rules('web_url','Site URL','trim|required|prep_url|xss_clean');
			
			$this->form_validation->set_rules('meta_title','Title','trim|required|xss_clean');
			$this->form_validation->set_rules('meta_keyword','Keyword','trim|required|xss_clean');
			$this->form_validation->set_rules('meta_description','Description','trim|required|xss_clean');
			
			$this->form_validation->set_rules('default_email','Default Email','trim|required|valid_email|xss_clean');
			$this->form_validation->set_rules('default_location','Default Location','trim|required|xss_clean');
            $this->form_validation->set_rules('currency','Currency','trim|required|xss_clean');
			
			if($this->form_validation->run() == true){
				
				$id = $this->input->post('id');
				
				$data=array(
						'web_title' => $this->input->post('web_title'),
						'tagline' => $this->input->post('tagline'),
						'web_url' => $this->input->post('web_url'),
						
						'meta_title' => $this->input->post('meta_title'),
						'meta_keyword' => $this->input->post('meta_keyword'),
						'meta_description' => $this->input->post('meta_description'),
						
						'google_analytics' => $this->input->post('google_analytics'),
						'adsense_pub_id' => $this->input->post('adsense_pub_id'),
						
						'facebook' => $this->input->post('facebook'),
						'twitter' => $this->input->post('twitter'),
						'google_plus' => $this->input->post('google_plus'),
						'linkedin' => $this->input->post('linkedin'),
						
						'default_email' => $this->input->post('default_email'),
						'call_now' => $this->input->post('call_now'),
						'default_location' => $this->input->post('default_location'),
                        'currency' => $this->input->post('currency'),
						'footer_text' => $this->input->post('footer_text'),
						
				);
				
			//	var_dump($data);die();
				
				$this->db->where('id',$id);
				$this->db->update('web_settings',$data);
				$msg = "Settings Updated Successfully...";
				$this->session->set_flashdata('message', $msg);
				redirect('secure/settings/web_settings');
			}
			
			
			
		}else{
			//$this->web_settings();
		}
	}

    function sitelogo(){

       $this->template->build('settings/sitelogo');

    }

    function do_upload() {

        if ($_FILES['userfile']['error'] == 0) {
            $ext = preg_replace("/.*\.([^.]+)$/", "\\1", $_FILES['userfile']['name']);
            //$filename= "sitelogo.".$ext;
            $filename = 'sitelogo';

            $config['file_name'] = $filename;
            $config['upload_path'] = './assets/img/';
            $config['allowed_types'] = 'jpg|png|gif|jpeg|';
            $config['overwrite'] = true;
            $config['remove_spaces'] = true;
            $this->load->library('upload', $config);
            if (!$this->upload->do_upload('userfile')) {
                $this->session->set_flashdata('message', $this->upload->display_errors('<p class="error">', '</p>'));
            }

            //--save--//

            $fname = $filename.".".$ext;
           // var_dump($fname);die();
            $this->db->query("delete from web_configuration where variable = 'site_logo'");


            $data = array(
                'description'	=> 'Site logo',
                'variable'		=> 'site_logo',
                'value'			=> $fname

            );

          //  var_dump($data);die();
            $this->db->insert('web_configuration',$data);

            redirect('secure/settings/sitelogo');
        } else {
            $this->session->set_flashdata('message', 'Please Choose a correct file');
            redirect('secure/settings/sitelogo');
        }
    }

}
?>